﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using elfam.web.Extensions;
using NHibernate.Criterion;

namespace elfam.web.Paging
{
    [Serializable]
    public class SearchCriteria 
    {
        [Link]
        public int? PageIndex { get; set; }

        [Link]
        public string SortExpression { get; set; }

        [Link]
        public string SortDirection { get; set; }
        public DetachedCriteria Criteria { get; set; }
        public List<NameValue> SearchParams { get; set; }

        public static string PageIndexProperty = "PageIndex";
        public static string SortFieldProperty = "SortField";
        public static string SortDirectionProperty = "SortDirection";
        public virtual string DefaultSearchField
        {
            get { return "Id"; }
        }
        public static string AscSearchOrder = "asc";
        public static string DescSearchOrder = "desc";
        public int PageSize = 20;

        public SearchCriteria()
        {
            SearchParams = new List<NameValue>();
        }

        public string SortFieldDefault()
        {
            if (string.IsNullOrEmpty(SortExpression))
                return DefaultSearchField;
            return SortExpression;
        }

        public NHibernate.Criterion.Order GetOrder()
        {
            if (string.IsNullOrEmpty(SortExpression))
                return NHibernate.Criterion.Order.Asc(SortFieldDefault());
            return SortDirection == AscSearchOrder ? NHibernate.Criterion.Order.Asc(SortFieldDefault()) : NHibernate.Criterion.Order.Desc(SortFieldDefault());
        }

        public int PageIndexDefault()
        {
            if (!PageIndex.HasValue)
                return 1;
            return PageIndex.Value;
        }



        public string SortDirectionDefault()
        {
            if (!string.IsNullOrEmpty(SortDirection))
                return SortDirection;
            return AscSearchOrder;
        }

        public void AddParam(NameValue param)
        {
            SearchParams.Add(param);
        }
        public int FirstResultIndex()
        {
            return (PageIndexDefault() - 1) * PageSize;
        }

        public SearchCriteria OrderBy(string sortExpression)
        {
            SearchCriteria another = this.Clone<SearchCriteria>();
            another.SortExpression = sortExpression;
            return another;
        }

        public SearchCriteria Asc()
        {
            SearchCriteria another = this.Clone<SearchCriteria>();
            another.SortDirection = AscSearchOrder;
            return another;
        }

        public SearchCriteria Desc()
        {
            SearchCriteria another = this.Clone<SearchCriteria>();
            another.SortDirection = DescSearchOrder;
            return another;
        }

        
    }
}
